package com.scsj.supermarket.view.activity.mainmodel;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.IndexGoodsListAdapter;
import com.scsj.supermarket.adapter.ShopListBaseAdapter;
import com.scsj.supermarket.adapter.SixRecommendAdapter;
import com.scsj.supermarket.base.IndexFuncBase;
import com.scsj.supermarket.bean.IndexGoodsListBean;
import com.scsj.supermarket.bean.IndexTenFuncBean;
import com.scsj.supermarket.bean.ShopListBaseBean;
import com.scsj.supermarket.bean.SixRecommendBean;
import com.scsj.supermarket.presenter.IndexFuncPresenter;
import com.scsj.supermarket.utils.GlideImageLoader;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 首页十个功能键统一调用页面，type分类设置title，和处理逻辑
 */

public class IndexTenFuncActivity extends BaseActivity implements IndexFuncBase.IView {

    private RecyclerView market_recommend_rv;
    private SixRecommendAdapter sixRecommendAdapter;
    private List<SixRecommendBean> sixRecommendBeanList;
    private RecyclerView market_func_rv;
    private ShopListBaseAdapter shopListBaseAdapter;
    private List<ShopListBaseBean.ShopListBase> shopListBaseBeanList;
    private IndexFuncPresenter presenter;

    private Banner banner1;
    private Banner banner2;


    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_index_ten_func);
        initToolbar();

        switch (getIntent().getIntExtra("type", 0)) {
            case 0:
                setToolbarTitle("商超专属");
                break;
            case 1:
                setToolbarTitle("生鲜果蔬");
                break;
            case 2:
                setToolbarTitle("酒水饮料");
                break;
            case 3:
                setToolbarTitle("家庭用品");
                break;
            case 4:
                setToolbarTitle("方便速食");
                break;
            case 5:
                setToolbarTitle("文具体育");
                break;
            case 6:
                setToolbarTitle("美食烘焙");
                break;
            case 7:
                setToolbarTitle("商超专送");
                break;
            case 8:
                setToolbarTitle("精选便利");
                break;
            case 9:
                setToolbarTitle("全部分类");
                break;
            case 10:
                setToolbarTitle("进口好货");
                break;
        }
        hasBack(true);

        presenter = new IndexFuncPresenter(this);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cityName", "郑州市");
        jsonObject.put("advertisingSpaceTemplate", "link_target_advertising_space");
        jsonObject.put("urlTargetExplain", "app_sczs");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonObject.toString());
        presenter.deliverAllList(requestBody);

        initView();
        initListener();
        initData();

    }

    @Override
    protected void initView() {

        banner1 = findViewById(R.id.banner1);
        banner2 = findViewById(R.id.banner2);

        market_recommend_rv = findViewById(R.id.market_recommend_rv);
        market_recommend_rv.setLayoutManager(new GridLayoutManager(this, 2));
        market_recommend_rv.setNestedScrollingEnabled(false);
        market_func_rv = findViewById(R.id.market_func_rv);
        market_func_rv.setLayoutManager(new LinearLayoutManager(this));
        market_func_rv.setNestedScrollingEnabled(false);

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {


    }

    @Override
    public void showSuccessMsg(String s) {
        Toasty.success(this, s).show();
    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this, s).show();
    }

    @Override
    public void setAllData(IndexTenFuncBean indexTenFuncBean) {

        /**
         * 轮播图1
         */
        ArrayList<String> banner1Imgs = new ArrayList<>();
        for (int i = 0; i < indexTenFuncBean.getData().get(0).getAppAdvertisingVoList().size(); i++) {
            banner1Imgs.add(indexTenFuncBean.getData().get(0).getAppAdvertisingVoList().get(i).getResourceInfo().getUrlFrameCapture());
        }
        banner1.setImages(banner1Imgs).setImageLoader(new GlideImageLoader()).start();

        /**
         * 轮播图2
         */
        ArrayList<String> banner2Imgs = new ArrayList<>();
        for (int i = 0; i < indexTenFuncBean.getData().get(2).getAppAdvertisingVoList().size(); i++) {
            banner2Imgs.add(indexTenFuncBean.getData().get(2).getAppAdvertisingVoList().get(i).getResourceInfo().getUrlFrameCapture());
        }
        banner2.setImages(banner2Imgs).setImageLoader(new GlideImageLoader()).start();

        /**
         * 上方推荐
         */
        sixRecommendBeanList = new ArrayList<>();
        for (int i = 0; i < indexTenFuncBean.getData().get(1).getAppAdvertisingVoList().size(); i++) {
            sixRecommendBeanList.add(new SixRecommendBean("溜溜梅", "20", indexTenFuncBean.getData().get(1).getAppAdvertisingVoList().get(i).getResourceInfo().getUrlFrameCapture()));
        }
        sixRecommendAdapter = new SixRecommendAdapter(sixRecommendBeanList);
        market_recommend_rv.setAdapter(sixRecommendAdapter);
        sixRecommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                view.findViewById(R.id.top_recommend_ll).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SkipUtils.toMarketDetail(mContext);
                    }
                });
            }
        });

        /**
         * 下方推荐
         */

        shopListBaseBeanList = new ArrayList<>();
        for (int i = 0; i < indexTenFuncBean.getData().get(3).getAppAdvertisingVoList().size(); i++) {

            shopListBaseBeanList = new ArrayList();
            HashMap map = new HashMap();
            map.put(0, "这是图片的url");
            map.put(1, "CBD商圈");
            map.put(2, "黑人牙膏，亮白银子 182/支");
            map.put(3, "美白两边，注重生活");
            map.put(4, "销量10");
            map.put(5, "收藏20");
            map.put(6, "¥26.52");
            map.put(7, "¥12.36");
            shopListBaseBeanList.add(new ShopListBaseBean.ShopListBase(map));

        }
        shopListBaseAdapter = new ShopListBaseAdapter(shopListBaseBeanList);
        market_func_rv.setAdapter(shopListBaseAdapter);
        shopListBaseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                view.findViewById(R.id.bottom_recommend_ll).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SkipUtils.toMarketDetail(mContext);
                    }
                });
            }
        });


    }
}
